package org.example.curriculum_design_6_16.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.curriculum_design_6_16.config.SocialServiceConfig;
import org.example.curriculum_design_6_16.entity.pojo.SocialServiceRule;
import org.example.curriculum_design_6_16.service.SocialServiceRuleService;
import org.example.curriculum_design_6_16.mapper.SocialServiceRuleMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author 郑凯
* @description 针对表【social_service_rule】的数据库操作Service实现
* @createDate 2025-06-19 09:13:24
*/
@Service
public class SocialServiceRuleServiceImpl extends ServiceImpl<SocialServiceRuleMapper, SocialServiceRule>
    implements SocialServiceRuleService{

    @Override
    public List<SocialServiceRule> getAllActiveRules() {
        return this.list(new QueryWrapper<SocialServiceRule>()
                .eq("is_active", SocialServiceConfig.ACTIVE_YES)
                .orderByAsc("rule_key"));
    }

    @Override
    public List<SocialServiceRule> getRulesByType(String ruleType) {
        return this.list(new QueryWrapper<SocialServiceRule>()
                .eq("rule_type", ruleType)
                .eq("is_active", SocialServiceConfig.ACTIVE_YES)
                .orderByAsc("rule_key"));
    }

    @Override
    public Map<String, SocialServiceRule> getPositionRuleMap() {
        List<SocialServiceRule> positionRules = getRulesByType(SocialServiceConfig.RULE_TYPE_POSITION);
        return positionRules.stream()
                .collect(Collectors.toMap(
                        SocialServiceRule::getRuleKey,
                        rule -> rule,
                        (existing, replacement) -> existing
                ));
    }

    @Override
    @Transactional
    public int addRule(SocialServiceRule rule) {
        // 设置默认值
        if (rule.getIsActive() == null) {
            rule.setIsActive(SocialServiceConfig.ACTIVE_YES);
        }
        if (rule.getDurationRequired() == null) {
            rule.setDurationRequired(SocialServiceConfig.DURATION_REQUIRED_NO);
        }
        
        return this.save(rule) ? 1 : 0;
    }

    @Override
    @Transactional
    public int updateRule(SocialServiceRule rule) {
        if (rule.getId() == null) {
            throw new IllegalArgumentException("规则ID不能为空");
        }
        
        SocialServiceRule existingRule = this.getById(rule.getId());
        if (existingRule == null) {
            throw new IllegalArgumentException("规则不存在");
        }
        
        return this.updateById(rule) ? 1 : 0;
    }

    @Override
    @Transactional
    public int toggleRuleStatus(Integer id, Boolean active) {
        if (id == null) {
            throw new IllegalArgumentException("规则ID不能为空");
        }
        
        SocialServiceRule rule = this.getById(id);
        if (rule == null) {
            throw new IllegalArgumentException("规则不存在");
        }
        
        rule.setIsActive(active ? SocialServiceConfig.ACTIVE_YES : SocialServiceConfig.ACTIVE_NO);
        return this.updateById(rule) ? 1 : 0;
    }

}




